<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="../css/bootstrap.min.css" rel="stylesheet" />
    <link href="../css/bootstrap-theme.min.css" rel="stylesheet" />
    <link href="../css/add.css" rel="stylesheet" />
    <link rel="stylesheet" href="../css/bootstrap-select.min.css">
    <link rel="stylesheet" href="../css/tailwind.min.css">

    <script src="../js/jquery-3.4.1.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script src="../js/bootstrap-select.min.js"></script>


    <title>添加商品</title>
</head>
<body>
    <div class="global" id="global">
        <div class="cm-middle">
            <div style="width: 100%; height: 150px;"></div>
            <div class="cm-middle-son">
                <div class="cm-middle-son">
                    <div class="cm-middle-all">
                        <div class="cm-middle-left">
                            <div class="cm-middle-row">
                                <label class="cm-middle-label">商品名：</label>
                                <div class="cm-middle-input">
                                    <input type="text" class="form-control ch" id="ComName">
                                </div>
                            </div>
                            <div class="cm-middle-row-useless"></div>
                            <div class="cm-middle-row">
                                <label class="cm-middle-label">现价：</label>
                                <div class="cm-middle-input">
                                    <input type="text" class="form-control ch" id="ComPrice">
                                </div>
                            </div>
                            <div class="cm-middle-row-useless"></div>
                            <div class="cm-middle-row">
                                <label class="cm-middle-label">原价：</label>
                                <div class="cm-middle-input">
                                    <input type="text" class="form-control ch" id="ComOprice">
                                </div>
                            </div>
                            <div class="cm-middle-row-useless"></div>
                            <div class="cm-middle-row">
                                <label class="cm-middle-label">商品类别 ：</label>
                                <select class="selectpicker">
                                    <option value="1">资料</option>
                                    <option value="2">数码</option>
                                    <option value="0" selected="selected">其他</option>
                                </select>
                            </div>
                            <div class="cm-middle-row-useless"></div>
                            <div class="cm-middle-row">
                                <label class="cm-middle-label">简介：</label>
                                <div class="cm-middle-input">
                                    <textarea class="form-control th" rows="5" id="ComIntro"></textarea>
                                </div>
                            </div>
                            <div class="cm-middle-row-useless"></div>
                            <div class="cm-middle-row-useless"></div>
                            <div class="cm-middle-row-useless"></div>
                            <div class="cm-middle-row-useless">
                                <div class="cm-middle-row-useless-left"></div>
                                <div class="cm-middle-row-useless-right" id="Tips" style="color: #ac2925;"></div>
                            </div>
                            <div class="cm-middle-row">
                                <button type="button" class="btn btn-default cm-middle-submit">确认修改</button>
                            </div>
                        </div>
                    </div>
                    <div class="cm-middle-all">
                        <div class="cm-middle-right" id="tp">点击此处重新上传商品图片

                        </div>
                        <input type="file" title="点击此处上传头像" id="file" onchange="show(this)" style="height: 238px;width: 298px;position:relative;top: -238px;opacity: 0;"/>
                        <!--opacity: 0;透明-->
                    </div>
                </div>
            </div>
        </div>
    </div>
<script type="text/javascript">
    var id;
    var ComImg;
    $(document).ready(function ()
    {
        id =  window.location.search.substr(1);
        $.get("/only/look",{"id": id},function (data)
        {
            document.getElementById('ComName').value = data.Cname;
            document.getElementById('ComPrice').value = data.Price;
            document.getElementById('ComOprice').value = data.Originalprice;
            document.getElementById('ComIntro').value = data.Intor;
            ComImg = data.Image;
        })
    })

    function show(file){
        var reader = new FileReader();	// 实例化一个FileReader对象，用于读取文件
        var tp = document.getElementById('tp'); 	// 获取要显示图片的标签
        tp.innerHTML = "<img id=\"img\" src=\"\">";
        var img = document.getElementById('img'); 	// 获取要显示图片的标签
        //读取File对象的数据
        reader.onload = function(evt){
            img.width  =  "300";
            img.height =  "360";
            img.src = evt.target.result;
        };
        reader.readAsDataURL(file.files[0]);

        sessionStorage.removeItem("file");//记录图片文件是否修改
        sessionStorage.setItem("file", "yes");
        //sessionStorage.getItem("file")
    }

    $("button").click(function(){
        var spm = $("input:eq(0)").val();
        var xj = $("input:eq(1)").val();
        var yj = $("input:eq(2)").val();
        var lb = $("select option:selected").text();
        var jj = $("textarea").val();

        if (spm === "" || xj === "" || yj === "")
        {
            $("#Tips").empty();
            $("#Tips").css("font-size","15px");
            $("#Tips").html("商品信息不能为空");
            return;
        }

        var F = sessionStorage.getItem("file");
        var form = new FormData();
        var file = $("#file")[0].files[0];
        var Cid = id;

        if (F=="yes")
        {
            ComImg = file;
        }

        var par = {"Cid":id,"Cname":spm, "Price":xj, "Originalprice":yj, "Intor":jj, "Classification":lb, "Image":ComImg}
        $.post("/ComAmend", par, function (result)
        {
            var F = sessionStorage.getItem("file");
            if(F=="yes")
            {
                form.append("file", file);
                form.append("Cid", Cid);
                form.append("SP", "c");
                $.ajax({
                    url:"/upload",
                    type:"post",
                    data:form,
                    processData:false,
                    contentType:false,
                    success:function(data){
                        if (data === true)
                        {
                            $("#Tips").empty();
                            $("#Tips").css("font-size","15px");
                            $("#Tips").html("商品添加成功");
                        }
                        else
                        {
                            $("#Tips").empty();
                            $("#Tips").css("font-size","15px");
                            $("#Tips").html("图片添加失败，请稍后刷新重试");
                        }
                    }
                });
            }
            else
            {
                if (result == true)
                {
                    alert("修改成功");
                    window.location.href="Own_Com_look.html?"+id;
                }else
                {
                    alert("修改失败");
                }
            }

        })
    });
</script>
</body>
</html>
